package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.LinearLayout
import android.widget.ProgressBar
import com.horoscope.astrology.zodiac.signs.free.R
import kotlinx.android.synthetic.main.activity_web_view.*
import kotlinx.android.synthetic.main.back_title.*


class WebViewActivity : Base2Activity() {

    private var webView: WebView? = null
    private var mProgressBar: ProgressBar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionWhite()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_view)
        initTitleBar(
            this@WebViewActivity,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            intent.getStringExtra(ARTICLE_TITLE)
        )
        text_view_back_title.setTextColor(Color.BLACK)
        btn_back_edit_name.setImageResource(R.drawable.icon_back_black)
        initView()
        buildWebView()
    }

    private fun buildWebView() {
        if (Build.VERSION.SDK_INT >= 19) {
            webView!!.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        webView!!.settings.javaScriptEnabled = true
        webView!!.settings.javaScriptCanOpenWindowsAutomatically = true
        webView!!.isHorizontalScrollBarEnabled = false
        webView!!.webChromeClient = MyWebChromeClient()
        webView!!.loadUrl(intent.getStringExtra(INTENT_KEY_URL))
    }

    private fun initView() {
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        // 代码中添加webview
        webView = MyWebView(this.applicationContext)
        webView!!.layoutParams = layoutParams
        ll_webView.addView(webView)
        mProgressBar = findViewById(R.id.progress_bar_web)
    }

    override fun onPause() {
        super.onPause()
        finish()
    }

    internal inner class MyWebChromeClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            if (newProgress == 100) {
                mProgressBar!!.visibility = View.GONE
            } else {
                mProgressBar!!.visibility = View.VISIBLE
                mProgressBar!!.progress = newProgress
            }
        }
    }

    companion object {
        private const val INTENT_KEY_URL = "intent_key_url"
        //        private const val JS_OBJECT_NAME = "client"
        private const val TYPE_HTML = "type_html"
        private const val ARTICLE_TITLE = "article title"

        fun generateWebViewIntent(
            context: Context,
            url: String?,
            isHtml: Boolean = false,
            title: String?
        ): Intent {
            val intent = Intent(context, WebViewActivity::class.java)
            intent.putExtra(INTENT_KEY_URL, url)
            intent.putExtra(TYPE_HTML, isHtml)
            intent.putExtra(ARTICLE_TITLE, title)
            return intent
        }
    }

    override fun onDestroy() {
        if (webView != null) {
            //加载null内容
            webView!!.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
            webView!!.stopLoading()
            //清除历史记录
            webView!!.clearHistory()
            //清除缓存
            webView!!.clearCache(true)
            //移除WebView
            ((webView!!.parent) as ViewGroup).removeView(webView)
            //销毁VebView
            webView!!.destroy()
            //WebView置为null
            webView = null
        }
        super.onDestroy()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView!!.canGoBack()) {
                webView!!.goBack()
                return true
            }
        }
        return super.onKeyDown(keyCode, event)
    }

    class MyWebView : WebView {
        constructor(context: Context?) : super(context)
        constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
        constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
        )

        //重写onScrollChanged 方法(隐私右侧有空白问题)
        override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
            super.onScrollChanged(l, t, oldl, oldt)
            scrollTo(0, t)
        }
    }

}